package flc.ast.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.adapter.FortuneAdapter;
import flc.ast.bean.FortuneBean;
import flc.ast.databinding.DialogFortuneStyleBinding;
import stark.common.basic.base.BaseSmartDialog;

public class FortuneDialog extends BaseSmartDialog<DialogFortuneStyleBinding> implements View.OnClickListener {


    private FortuneAdapter fortuneAdapter;
    public int selPosition;

    public FortuneDialog(@NonNull Context context, int position) {
        super(context);
        selPosition = position;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_fortune_style;
    }

    @Override
    protected void initView(View view) {
        List<FortuneBean> list = new ArrayList<>();
        String[] str = getContext().getResources().getStringArray(R.array.fortune);
        for (String s : str) {
            list.add(new FortuneBean(s));
        }
        list.get(selPosition).setSelected(true);

        mDataBinding.rvFortuneList.setLayoutManager(new LinearLayoutManager(getContext()));
        fortuneAdapter = new FortuneAdapter();
        mDataBinding.rvFortuneList.setAdapter(fortuneAdapter);
        fortuneAdapter.setList(list);
        fortuneAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                fortuneAdapter.getItem(selPosition).setSelected(false);
                selPosition = position;
                fortuneAdapter.getItem(position).setSelected(true);
                fortuneAdapter.notifyDataSetChanged();
                listener.click(position);
                dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    private Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public interface Listener {
        void click(int position);
    }
}
